package com.zhongke.cotrun.controller;

import javax.validation.constraints.NotNull;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.PayMallInfo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.PayMallInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

 /**
 * @Description: 商品基本信息管理
 * @Author: songjing
 * @Date:   2020-03-20
 * @Version: V1.0
 */
@Api(tags = "商品基本信息管理")
@RestController
@RequestMapping("/mallInfo")
@Validated
public class PayMallInfoController {
	@Autowired
	private PayMallInfoService payMallInfoService;
	
	/**
	  * 分页列表查询
	 * @param rqRoom
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "查看商品列表")
    @GetMapping(value= {"/selectAll"},produces = "application/json;charset=utf-8")
	public APIResponse selectAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize) {
		Page<PayMallInfo> page = new Page<PayMallInfo>(pageNum, pageSize);
		IPage<PayMallInfo> pageList = payMallInfoService.page(page);
		return APIResponse.returnSuccess(pageList);
	}
	/**
	 *   添加
	 * @param PayMallInfo
	 * @return
	 */
	@ApiOperation(value = "添加商品")
	@PostMapping(value = "/add")
	public APIResponse add(PayMallInfo payMallInfo) {		
		try {
			//直接使用sql语句新增，系统时间（now）可不填写，自动默认
			//{主体主键-mainId},#{类型主键，暂定1-typeId},#{商品标题-title},#{商品价格-price}，#{商品库存-stock},#{发布人主键--createBy}"
			payMallInfoService.cotrunInsert(payMallInfo);
			
			//使用对象直接赋值，注意需要set当前系统时间和操作人主键
			/*payMallInfo.setCreateDate(new Date());
			payMallInfoService.save(payMallInfo);*/
			return APIResponse.returnSuccess("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
		}
	}
	
	/**
	  *  编辑
	 * @param PayMallInfo
	 * @return
	 */
	@ApiOperation(value = "编辑商品")
	@PostMapping(value = "/update")
	public APIResponse update(PayMallInfo payMallInfo) {		
		PayMallInfo payMallInfoC = payMallInfoService.getById(payMallInfo.getId());
		if(payMallInfoC==null)
			return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
		
		if(payMallInfoService.updateById(payMallInfo))
			return APIResponse.returnSuccess("编辑成功");
		else
			return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
	}
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除指定商品")
	@GetMapping(value= {"/delete"},produces = "application/json;charset=utf-8")
	public APIResponse delete(@NotNull(message = "商品主键不能为空") Long id) {
		try {
			PayMallInfo payMallInfo = payMallInfoService.getById(id);
			if(payMallInfo == null)
				return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
			
			payMallInfo.setDelFlag("1");
			payMallInfoService.updateById(payMallInfo);
			
			return APIResponse.returnSuccess("删除成功");
		} catch (Exception e) {
			return APIResponse.returnFail(CodeMsg.DELECT_ERROP);
		}
	}
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "查看指定商品详情")
	@GetMapping(value = "/selectDetail")
	public APIResponse selectDetail(@NotNull(message = "商品主键不能为空") Long id) {		
		PayMallInfo payMallInfo = payMallInfoService.getById(id);
		if(payMallInfo==null)
			return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
		else
			return APIResponse.returnSuccess(payMallInfo);
	}
}
